import _ from 'lodash'
// interface A{
// fn: (value: number)=>string
// }
// interface B{
// fn: (value: string)=>number
// }
// type C=A & B
// let c: C
// c.fn(1)
//泛型
function id<Type>(value: Type ): Type{
    return value
}
const num=id<number>(10)
const str=id<string>('a')
const ret=id<boolean>(false)
let str1 = id('abc')
//泛型约束
function id1<Type>(value: Type[] ): Type[]{
    console.log(value.length);
     return value
}
id1([1,2])
interface ILength{length: number}
function id2<Type extends ILength>(value: Type ): Type{
    console.log(value.length);
     return value
}
id2(['1','33'])
id2('hellos')
//多个泛型变量
function getProp<Type, Key extends keyof Type>(obj: Type,key: Key) {
  return obj[key]
}
getProp({name:'张三',age:12},'age')
getProp(14,'toFixed')
//泛型接口
interface IdFunc<Type>{
    id: (value: Type)=>Type
    ids: ()=>Type[]
}
let obj: IdFunc<number>={
id(value){
return value
},
ids(){
    return [22,33]
}
}
let arr=[11,22,33]
arr.forEach(val=>{console.log(val)})
//泛型类
class GenericNumber<NumType>{
defaultValue: NumType
// add: (x: NumType,y: NumType)=>NumType
constructor(value: NumType){
this.defaultValue=value
}
}
const myNum=new GenericNumber(10)

//泛型工具类型 Partial Readonly  Pick Record
 interface Props{
    id: string
    children: number[]
 }
 type PartialProps=Partial<Props>
 let p2: PartialProps={id :'1'}
 type ReadonlyProps=Readonly<Props>
 let p3: ReadonlyProps={id :'1',children:[22]}
 type PickProps=Pick<Props,'id' >
 let p4: PickProps={id:'3'}
 //Record<Keys,Type> 构造一个对象类型，属性键为Keys，属性类型为Type
 type RecordObj=Record<'a' |'b' |'c',string[]>
 let obj1: RecordObj={
    a:['hi'],
    b:['hello'],
    c:['hahha']
 }
//索引签名类型
interface AnyObject{
    [key: string]: number
}
let ojb2: AnyObject={
    a:1,
    b:2
}
//映射类型
type PropsKeys='x' |'y' |'z'
type Type1={x: number,y: number,z: number}
//等同于
type Type2={[Key in PropsKeys]:number}

type Props2 = { a: number; b: string; c: boolean }
type Type3 = { [key in keyof Props2]: number }
//索引查询类型
type TypeA=Props2['a']
type TypeAll=Props2[keyof Props2]